<template>
    <el-container class="home-container">
        <el-header>
            <div>
                <img
                    src="../assets/avatar.jpg"
                    alt=""
                >
                <span>电商后台管理系统</span>
            </div>
            <el-button
                type="info"
                @click="logout"
            >退出登录</el-button>
        </el-header>
        <el-container>
            <el-aside width="200px">

                <el-menu
                    background-color="#333744"
                    text-color="#fff"
                    active-text-color="#409EFF"
                    :unique-opened = "true"
                    router
                    :default-active="activeIndex"
                >
                    <!--一级导航-->
                    <el-submenu
                        :index="item.id + ''"
                        v-for="item in menuList"
                        :key="item.id"
                    >
                        <template slot="title">
                            <i :class="iconsObj[item.id]"></i>
                            <span>&nbsp;&nbsp;{{ item.authName }}</span>
                        </template>
                        <!--二级导航-->
                        <el-menu-item
                            :index="'/' + secItem.path"
                            v-for="secItem in item.children"
                            :key="secItem.id"
                        >
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{secItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>

                </el-menu>
            </el-aside>
            <el-main>

                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      activeIndex:''
    };
  },
  mounted() {
    this.getMenuList();
    this.activeIndex = this.$route.path
  },
  // 两个bug
  // 1 刷新导航栏丢失选中状态
  // 2 前进或者后退 导航栏无法与页面同步
  methods: {
    logout() {
      // 清空本地的token
      window.sessionStorage.clear();
      // 跳转回login
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http.get(
        "http://127.0.0.1:8888/api/private/v1/menus"
      );
      if (res.meta.status !== 200) {
        return this.$message.error("获取左侧导航数据失败");
      }
      this.menuList = res.data;
    }
  },
  watch:{
      "$route.path":function(val){
          this.activeIndex = val
      }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;

    img {
      width: 70px;
    }

    > div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    .el-menu {
        border-right: none
    }
  }

  .el-main {
    background-color: #eaedf1;
  }
}
</style>
